package dcsinstnce.server.interfaces.impl;

import java.util.Vector;

import dcsinstnce.server.interfaces.PublisherOperations;
import dcsinstnce.server.interfaces.Suscriber;
import dcsinstnce.server.operations.Operation;


public class PublisherImpl extends Thread implements PublisherOperations{

	private Vector<Operation> operations;
	private Vector<String> operationsName;
	private Semaphore semaphore;
	private Suscriber suscriber;
	/**
	 * constructeur
	 * @param suscriber
	 */
	public PublisherImpl(Suscriber suscriber) {
		operations = new Vector<Operation>();
		operationsName = new Vector<String>();
		this.suscriber = suscriber;
		semaphore = new Semaphore();
	}
	
	@Override
	public void run() {
		while(true){
			if(operations.size() == 0)
				semaphore.stop();
			else{
				suscriber.publish(operationsName.remove(0), operations.remove(0));
			}
		}
	}
	
	@Override
	synchronized public boolean addData(String operationName, Operation operation) {
		semaphore.signal();
		return operations.add(operation) && operationsName.add(operationName);
	}

}
